<?php

namespace app\promotion\controller\agent;
use app\common\controller\BaseController;
use app\common\service\merchant\MerchantService;
use app\common\service\merchant\MerchantShopService;
use app\common\service\promotion\PromotionUserService;
use app\common\validate\merchant\MerchantValidate;
use hg\apidoc\annotation as Apidoc;
use think\Request;

/**
 * @Apidoc\Title("商户管理")
 * @Apidoc\Group("merchant")
 * @Apidoc\Sort("250")
 */
class Merchant extends BaseController
{
    /**
    * @Apidoc\Title("商户管理列表")
    * @Apidoc\Query(ref="pagingQuery")
    * @Apidoc\Query(ref="sortQuery")
    * @Apidoc\Query(ref="searchQuery")
    * @Apidoc\Query(ref="dateQuery")
    * @Apidoc\Returned(ref="expsReturn")
    * @Apidoc\Returned(ref="pagingReturn")
    * @Apidoc\Returned("list", type="array", desc="商户管理列表", children={
    *   @Apidoc\Returned(ref="app\common\model\Merchant\MerchantModel", field="id,create_time,update_time")
    * })
    */
    public function list()
    {
        $where = $this->buildWhere([
            'mer_account',
            'member_id',
            'merchant_pid',
            'is_disable'
        ]);
        $where = $this->where(where_delete($where));
        $where[] = ['promotion_user_id','=',promotion_user_id()];
        $data = MerchantService::getListByPromotion($where, $this->page(), $this->limit(), $this->order());
        return success($data);
    }

    /**
     * @Apidoc\Title("选择商家")
     * @Apidoc\Query(ref="sortQuery")
     * @Apidoc\Query(ref="searchQuery")
     * @Apidoc\Query(ref="dateQuery")
     * @Apidoc\Returned(ref="app\common\model\Merchant\MerchantModel", field="id,title")
 */
    public function select()
    {
        $where = $this->where(where_delete());
        $data = MerchantService::list($where,0,0, $this->order(),'id as value,title as label,is_disable as disable');
        //处理商家编码
        foreach ($data as $key=>&$val){
            $val['label'] = $val['value'].($val['label']?".".$val['label']:"");
        }
        return success($data);
    }

    /**
    * @Apidoc\Title("商户管理信息")
    * @Apidoc\Query(ref="app\common\model\Merchant\MerchantModel", field="delete_time")
    * @Apidoc\Returned(ref="app\common\model\Merchant\MerchantModel")
    */
    public function info()
    {
        $param = $this->params(['id/d' => '']);
        validate(MerchantValidate::class)->scene('info')->check($param);
        $where = $this->where(where_delete());
        $where[] = ['merchant_id','=',$param['id']];
        $data = MerchantShopService::getListByPromotion($where, $this->page(), $this->limit(), $this->order());
        return success($data);
    }


    /**
     * @Apidoc\Title("查询参数")
     * @Apidoc\Query(ref="app\common\model\Merchant\MerchantModel", field="delete_time")
     * @Apidoc\Returned(ref="app\common\model\Merchant\MerchantModel")
     */
    public function getParams(Request $request)
    {
        $domain = $request->domain();
        $data['url'] = PromotionUserService::getInvitationLink($domain);
        return success($data);
    }
}
